<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/include/tag.jsp"%>
<%@ include file="/WEB-INF/include/common.jsp"%>
<link rel="stylesheet" href="${ctx}/statics/css/custom_theme.css" />
<style type="text/css">
	.a{background-color: red}
</style>
<div class="modal fade" id="brandRoleModel" tabindex="-1" role="dialog" aria-labelledby="brandRoleModelLabel" aria-hidden="true" data-backdrop="false">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                	<span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="brandRoleModelLabel">${subTitle}</h4>
            </div>
            <div class="modal-body">
        		<div id="brandRoleModel_modal_body"></div>
            </div>
        </div>
    </div>
</div>
<!-- 模态窗口 -->
<div class="modal fade" id="brandModel" tabindex="-1" role="dialog" aria-labelledby="brandModelLabel" aria-hidden="true" data-backdrop="false">
  <div class="modal-dialog" >
        <div class="modal-content">
            <div class="modal-header">
            	<button type="button" class="close" data-dismiss="modal" aria-label="Close">
                	<span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" >添加品牌</h4>
            </div>
            <div class="modal-body">
            	<form id="brand_edit_form" class="form-horizontal" enctype="multipart/form-data">
			        <input type="hidden" name="id">
			        <div class="form-group">
			        	<label class="control-label col-sm-3">名称：</label>
			        	<div class="col-sm-8">
				            <input type="text" class="form-control" name="name" >
			        	</div>
		        	</div>
		        	<div class="form-group">
			        	<label class="control-label col-sm-3">英文名称：</label>
			        	<div class="col-sm-8">
				            <input type="text" class="form-control" name="enName" >
			        	</div>
			        </div>
			        <div class="form-group">
			        	<label class="control-label col-sm-3">首字母：</label>
			        	<div class="col-sm-8">
				            <input type="text" class="form-control" name="initial">
			        	</div>
			        </div>
			        <div class="form-group">
		        		<label class="control-label col-sm-3">上传logo</label>
		        		<div class="col-sm-8">
		        			<input type="file" name="contractFile" class="file"/>
		        		</div>
			        </div>
			         <div class="form-group">
			        	<label class="control-label col-sm-3">描述：</label>
			        	<div class="col-sm-8">
				          <textarea name="remark" rows="2" cols="49"></textarea>
			        	</div>
			        </div>
			    </form>
            </div>
            <div class="modal-footer">
            	<button type="button" class="btn btn-sm btn-primary" id="brandModelBtn">确定</button>
                <button type="button" class="btn btn-sm btn-default" data-dismiss="modal" id="Close">关闭</button>
            </div>
        </div>
   </div>
</div>

<div id="main-content">
	<div class="panel panel-default">
		<div class="panel-heading">${title}</div>
		<!-- /.panel-heading -->
		<div class="panel-body" >
			<!-- PAGE CONTENT BEGINS -->
			<form class="form-horizontal" role="form" id="brand_search_form">
				<div class="col-sm-9">
					<label class="col-sm-1 control-label">品牌</label>
					<div class="col-sm-3">
						<input id="search_realName" name="name" type="text"/> 
					</div>
				</div>
				<div class="col-sm-3">
					<div class="col-sm-4">
						<button class="btn btn-sm btn-info" type="submit">
							<i class="icon-search nav-search-icon"></i>查询
						</button> 
					</div>
					<div class="col-sm-4"> 
						<button type="button" class="btn btn-success btn-sm" onclick="addData()"><i class="icon-plus bigger-110"></i>添加</button>
					</div>
					<div class="col-sm-4">
						<button class="btn btn-sm btn-primary" type="reset"	>
							<i class="icon-undo bigger-110"></i>清除
						</button> 
					</div>
				</div>
			</form>
			<div id="brand_table_tools">
				<div class="text-right">
				</div>
			</div>
			<table id="brand_table" class="table table-bordered table-hover table-condensed">
			</table>
		</div>
	</div>
</div>

<script type="text/javascript">
$(function(){
	$('#brand_table').bootstrapTable({
		url: '${ctx}/business/car/getBrandList.do',
		classes :"table-condensed",
		pagination: true,
		paginationLoop: false,
		smartDisplay: false,
		striped: true,
	    pageSize: 10,
	    pageList: [10, 20, 50, 100],
	    sidePagination: 'server',
	    showRefresh: false,
	    showColumns: false,
	    paginationNextText:'下一页',
	    paginationPreText:'上一页',
	    cache: false,
		columns: [{
			field: '',
			title: '操作',
			align: 'center',
			width:'100px',
			formatter: function(value, row, index){
				return '<button class="btn btn-xs btn-info" id="editData" title="编辑"><i class="icon-edit bigger-130"></i></button>'
						+'<button class="btn btn-xs btn-danger" id="deleteData" title="删除"><i class="icon-trash bigger-130"></i></button>';
						
			
			},
			events: {
				'click #editData': editData,
				'click #deleteData': deleteData
			}
			
		},{
			field: 'id',
			title: 'ID',
			align:"center"
		},{
			field: 'name',
			title: '名称',
			align:"center"
		},{
			field: 'enName',
			title: '英文',
			align:"center"
		},{
			field: 'imageUrl',
			title: 'logo',
			align: 'center',
			formatter: function(value, row, index){
				if(row.imageUrl!=null && row.imageUrl!=""){
					return html='<img src="'+row.imageUrl+'" style="height:30px;"/>';
				}else{
					return "";
				}
			}
		},{
			field: 'initial',
			title: '首字母',
			align:"center"
		},{
			field: 'creatime',
			title: '创建时间',
			align:"center"
		},{
			field: 'remark',
			title: '描述',
			align:"center"
		}],
		queryParams: function(params){
			//console.log(params);
			//params.usreId
			$.extend(params,$("#brand_search_form").serializeJson())
			return params;
		}
		
		
	});

	$('#brand_table').on('mouseenter','.pannel_a_title',function () {
        $(this).popover('show');
	});
	// 新规则要求值必须与第一个参数相同
	$.validator.addMethod("letter",function(value,element,param){ 
		var ret = /^[a-zA-Z\s]*[a-zA-Z\s]$/;
		return this.optional( element ) || ret.test(value);
	},"请输入正确的英文名称");
	
	$.validator.addMethod("english", function(value, element) {
	    var chrnum = /^([a-zA-Z]+)$/;
	    return this.optional(element) || (chrnum.test(value));
	}, "只能输入字母");
	//添加和编辑请求验证
	$("#brand_edit_form").validate({
		rules: {
			name:{required:true},
			enName:{required:true,letter:true},
			initial:{required:true,english:true,maxlength:1}
		},
		submitHandler: function(form) {
			$("#brandModel").prop("disabled",true);
    		$(form).ajaxSubmit({
    			type:"post",
    			dataType:"json",
    			url:"${ctx}/business/car/saveOrUpdateBrand.do",
    			success:function(data){
    				if(data.success==true){
    					$('#brandModel').modal('hide') 
    					$('#brand_table').bootstrapTable('refresh');
    				}else{
    					alert(data.errorMessage);
    				}
    				$("#brandModel").prop("disabled",false);
                }  
            });
		}
	});
	
	//提交用户信息
	$("#brandModel").on('click','#brandModelBtn',function(){
		$("#brand_edit_form").submit();
	});
	
	//搜索角色
	$("#brand_search_form").submit(function(e){
		e.preventDefault();
		$('#brand_table').bootstrapTable('refresh');
	});
	
});

function addData(e, v, row) {
	$("#brand_edit_form").resetForm();
	$("#brand_edit_form input[name=id]").val("");
	$("#brand_edit_form input[name=name]").val("");
	$("#brand_edit_form input[name=enName]").val("");
	$("#brand_edit_form input[name=initial]").val("");
	$("#brand_edit_form textarea[name=remark]").text("");
	$('#brandModel').modal('show');
}

//编辑用户
function editData(e, v, row) {
	$("#brandModel .modal-title").empty().html("编辑品牌");
	$("#brand_edit_form").resetForm();
	$("#brand_edit_form input[name=id]").val(row.id);
	$("#brand_edit_form input[name=name]").val(row.name);
	$("#brand_edit_form input[name=enName]").val(row.enName);
	$("#brand_edit_form input[name=initial]").val(row.initial);
	$("#brand_edit_form textarea[name=remark]").text(row.remark);
	$('#brandModel').modal('show');
}

//删除用户
function deleteData(e, v, row) {
	
	var r = confirm("你确定删除“"+row.name+"”吗？");
	if(r){
		$.ajax({
         url: "${ctx}/business/car/deleteById.do",
         type: "POST",
         data: {
             "id": row.id,
             "type":"brand"
         }, 
         success: function (data) {
         	if(data.success){
         		$('#brand_table').bootstrapTable('refresh');
             }else {
             	alert(data.errorMessage);
             }
         }
   		});
	}
	
}


</script>